var StorageOrg_row = function() {

	var left_margin_step = 15;
	var currentStorageOrgObj = null;
	//"close"样式(-)是已经展开时的样式
	// 控制方法：
	// 如果当前节点尚未展开(+)，此次点击触发下列行为：(1)将当前节点样式设置为已展开；(2)从后台查数据，渲染子节点;(3)将同辈的其他已展开节点及其子节点收缩，且将收缩节点的样式设置为收缩样式
	// 如果当前节点已经展开(-)，此次点击触发下列行为：(1)将当前节点的子节点收缩；(2)当前节点的样式设置为展开(-)
	$(".category_trigger9").live(
			'click',
			function() {
				var category_triggerObj=$(this);
				if(category_triggerObj.hasClass("close")){
					category_triggerObj.parent(".tree_category9").next(".children").slideUp(function(){
						category_triggerObj.removeClass("close");
					});
					return false;
				}
				$(this).addClass("close");
				var pk_storageorg_val = category_triggerObj.next("input").val();
				var myts = new Date();
				//var formStr = $('#org_filter_row_form').serialize();
				var formStr = $(this).parents("form").find("#storageOrg_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context
							+ "/other/qryStorageByPk?pk_storageorg="
							+ pk_storageorg_val 
							+ "&myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						var orgArr = eval(result);
						var current_margin_left_size = parseInt(category_triggerObj.attr("margin")) + left_margin_step;	
						var childrenDiv = $("<div></div>").addClass("children");
						for ( var loop = 0; loop < orgArr.length; loop++) {
							var obj = orgArr[loop];
							var a = $("<a></a>").attr("href", "#")
										.addClass("category_trigger9")
										.attr("style","margin-left:"+current_margin_left_size+"px")
										.attr("margin", current_margin_left_size);
							var inputObj = $("<input/>")
											.val(obj.pk_org)
											.attr("type", "radio")
											.attr("name", "pk_storageOrg_intree");
							if(!obj.orgtype9){
								inputObj.attr("disabled", "disabled");
							}
							var label = $("<label></label>").text(obj.name);
							var inputObj1 = $("<input/>").val(obj.pk_org).attr("type", "hidden");	
							var div = $("<div></div>")
									.addClass("tree_category9").append(a)
									.append(inputObj).append(label).append(inputObj1);
							childrenDiv.append(div);
						}
						childrenDiv.insertAfter(category_triggerObj.parent());
					}
				});
				
				//自身展开后，将其兄弟节点及其子节点收缩回
				category_triggerObj.parent(".tree_category9").next(".children").slideDown();
				if(category_triggerObj.parent(".tree_category9").length>0&&category_triggerObj.parent(".tree_category9").siblings(".tree_category9").length>0){
				 category_triggerObj.parent(".tree_category9").siblings(".tree_category9").next(".children").slideUp(function(){
					 $(this).prev(".tree_category9").find(".category_trigger9").removeClass("close");
				 });
				}
				return false;

			});
	
	$(".tree_btns9 .idBtnPicked").live('click', function() {
		//var items = $(this).parents(".ui-drop_dialog").find("[name='pk_storageOrg_intree']");
		var items = $("#storageOrgRowPicker").find("[name='pk_storageOrg_intree']");
		if(items != null){
			var str="";
			var pks="";
			for(var loop=0; loop<items.length; loop++){
				if(items[loop].checked == true){					
					str += $(items[loop]).next("label").html();
					pks += $(items[loop]).next("label").next("input").val();
					break;
				}
			}
			currentStorageOrgObj.css("color","black").val(str);
			currentStorageOrgObj.next("input").val(pks);
			//$("#pk_storageOrg").val(str.substring(0, length1-1));
			
			//$("#pk_storageorg").val(pks.substring(0, length2-1));
		}
		
		//$(this).parents(".ui-drop_dialog").slideUp();
		$("#storageOrgRowPicker").slideUp();
		return 0;
		
	});
	
	$(".tree_btns9 .idBtnCancel").live('click', function(){
		//$(this).parents(".ui-drop_dialog").slideUp();
		$("#storageOrgRowPicker").slideUp();
	})

	this.init = function() {
		$("body").delegate(
				"[name='storageOrg_name']",
				"click",
				function() {
					currentStorageOrgObj = $(this);
					var myts = new Date();
					//var formStr = $('#org_filter_row_form').serialize();
					var formStr = $(this).parents("form").find("#storageOrg_filter").serialize();
					jQuery.ajax( {
						type : "POST",
						async : false,
						url : ebvp_context + "/other/qryAllStorage?myts=" + myts,
						dataType : "text",
						data : "&" + formStr,
						success : function(result) {
							if(result != "[]"){
								var orgArr = eval(result);
								$("#storageOrgRowPicker").find(".tree_list9").empty();
								for ( var loop = 0; loop < orgArr.length; loop++) {
									var obj = orgArr[loop];
//									var a = $("<a></a>")
//												.attr("href", "#")
//												.addClass("category_trigger9")
//												.attr("margin", 0);								
									var inputObj = $("<input/>")
														.val(obj.pk_org)
														.attr("type", "radio")
														.attr("name", "pk_storageOrg_intree");	
//									if(!obj.orgtype9){
//										inputObj.attr("disabled", "disabled");
//									}
									
									var label = $("<label></label>").text(obj.name);
									var inputObj1 = $("<input/>")
														.val(obj.pk_org)
														.attr("type", "hidden");	
									var div = $("<div></div>")
											.addClass("tree_category9")
//											.append(a)
											.append(inputObj).append(label).append(inputObj1);												
									$("#storageOrgRowPicker").find(".tree_list9").append(div);
									
									
								}
							}
						}				
					})
					var position = $(this).parents("tr").find("[name='storageOrg_name']").offset();
					position.top += 20;
					$("#storageOrgRowPicker").slideDown("normal");
					$("#storageOrgRowPicker").offset(position);
				});
	}
	
	$("#filterOrg").bind(
			'click',
			function() {
				currentStorageOrgObj = $(this);
				var myts = new Date();
				//var formStr = $('#org_filter_row_form').serialize();
				var formStr = $(this).parents("form").find("#storageOrg_filter").serialize();
				jQuery.ajax( {
					type : "POST",
					async : false,
					url : ebvp_context + "/other/filterOrgs?myts=" + myts,
					dataType : "text",
					data : "&" + formStr,
					success : function(result) {
						if(result != "[]"){
							var orgArr = eval(result);
							$("#storageOrgRowPicker").find(".tree_list9").empty();
							for ( var loop = 0; loop < orgArr.length; loop++) {
								var obj = orgArr[loop];
//								var a = $("<a></a>")
//											.attr("href", "#")
//											.addClass("category_trigger9")
//											.attr("margin", 0);								
								var inputObj = $("<input/>")
													.val(obj.pk_org)
													.attr("type", "radio")
													.attr("name", "pk_storageOrg_intree");
//								if(!obj.orgtype9){
//									inputObj.attr("disabled", "disabled");
//								}
								var label = $("<label></label>").text(obj.name);
								var inputObj1 = $("<input/>")
													.val(obj.pk_org)
													.attr("type", "hidden");	
								var div = $("<div></div>")
										.addClass("tree_category9")
//										.append(a)
										.append(inputObj).append(label).append(inputObj1);												
								$("#storageOrgRowPicker").find(".tree_list9").append(div);
							}
						}
					}				
				})
			});		
}

$(function() {
	var storageOrg_row = new StorageOrg_row();
	storageOrg_row.init();
});
